<template>
  <el-dialog
    :title="!dataForm.attrId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="属性名" prop="attrName">
        <el-input v-model="dataForm.attrName" placeholder="属性名"></el-input>
      </el-form-item>
      <el-form-item label="可检索" prop="searchType">
        <el-switch
          v-model="dataForm.searchType"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="属性图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
      </el-form-item>

      <el-form-item label="启用状态" prop="enable">
        <el-switch
          v-model="dataForm.enable"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="所属榜单分类id" prop="catId">
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
      </el-form-item>
      <el-form-item label="所属分组" prop="groupId">
        <el-select
          ref="groupSelect"
          v-model="dataForm.groupId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in groups"
            :key="item.groupId"
            :label="item.groupName"
            :value="item.groupId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="值类型：允许多个值时不支持检索，隐藏掉是否检索选项" prop="valueType">
        <el-switch
          v-model="dataForm.valueType"
          active-text="允许多个值"
          inactive-text="只能单个值"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        ></el-switch>
      </el-form-item>
      <el-form-item label="可选值" prop="valueSelect">
        <!-- <el-input v-model="dataForm.valueSelect"></el-input> -->
        <el-select
          v-model="dataForm.valueSelect"
          multiple
          filterable
          allow-create
          placeholder="请输入内容"
        ></el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascader from "../common/category-cascader";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        attrId: 0,
        attrName: "",
        searchType: "",
        icon: "",
        valueSelect: "",
        enable: "",
        catId: "",
        catName: "",
        groupId: "",
        valueType: 1,
      },
      catelogPath: [],
      groups: [],
      dataRule: {
        attrName: [
          { required: true, message: "属性名不能为空", trigger: "blur" },
        ],
        searchType: [
          {
            required: true,
            message: "是否需要检索:0否 1是不能为空",
            trigger: "blur",
          },
        ],
        icon: [
          { required: true, message: "属性图标不能为空", trigger: "blur" },
        ],
        valueSelect: [
          { required: true, message: "可选值不能为空", trigger: "blur" },
        ],
        enable: [
          {
            required: true,
            message: "启用状态：0否 1是不能为空",
            trigger: "blur",
          },
        ],
        catId: [
          {
            required: true,
            message: "所属榜单分类id不能为空",
            trigger: "blur",
          },
        ],
        catName: [
          {
            required: true,
            message: "所属榜单分类名称不能为空",
            trigger: "blur",
          },
        ],
        groupId: [
          { required: true, message: "所属分组不能为空", trigger: "blur" },
        ],
        valueType: [
          { required: true, message: "值类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: { CategoryCascader },
  watch: {
    catelogPath(path) {
      //监听到路径变化需要查出这个三级分类的分组信息
      console.log("路径变了", path);
      this.groups = [];
      this.dataForm.groupId = "";
      this.dataForm.catId = path[path.length - 1];
      if (path && path.length == 3) {
        this.$http({
          url: this.$http.adornUrl(
            `/rank/attrgroup/list/${path[path.length - 1]}`
          ),
          method: "get",
          params: this.$http.adornParams({ page: 1, limit: 10000000 }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.groups = data.page.list;
          } else {
            this.$message.error(data.msg);
          }
        });
      } else if (path.length == 0) {
        this.dataForm.catId = "";
      } else {
        this.$message.error("请选择正确的分类");
        this.dataForm.catId = "";
      }
    },
  },
  methods: {
    init(id) {
      this.dataForm.attrId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.attrId) {
          this.$http({
            url: this.$http.adornUrl(`/rank/attr/info/${this.dataForm.attrId}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.attrName = data.attr.attrName;
              this.dataForm.searchType = data.attr.searchType;
              this.dataForm.icon = data.attr.icon;
              this.dataForm.valueSelect = data.attr.valueSelect.split(";");
              this.dataForm.enable = data.attr.enable;
              this.dataForm.catId = data.attr.catId;
              this.dataForm.catName = data.attr.catName;
              this.dataForm.groupId = data.attr.groupId;
              this.dataForm.valueType = data.attr.valueType;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          console.log(this.dataForm.valueSelect);
          this.$http({
            url: this.$http.adornUrl(
              `/rank/attr/${!this.dataForm.attrId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              attrId: this.dataForm.attrId || undefined,
              attrName: this.dataForm.attrName,
              searchType: this.dataForm.searchType,
              icon: this.dataForm.icon,
              valueSelect: this.dataForm.valueSelect.join(";"),
              enable: this.dataForm.enable,
              catId: this.dataForm.catId,
              catName: this.dataForm.catName,
              groupId: this.dataForm.groupId,
              valueType: this.dataForm.valueType,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },

    dialogClose() {
      this.catelogPath = [];
    },
  },
};
</script>
